package com.pzh.logistics.controller;


import com.pzh.logistics.common.ResultVO;
import com.pzh.logistics.common.constant.OrderConstant;
import com.pzh.logistics.common.constant.RoleConstant;
import com.pzh.logistics.mbg.model.Address;
import com.pzh.logistics.mbg.model.User;
import com.pzh.logistics.service.CourierService;
import com.pzh.logistics.service.OrderService;
import com.pzh.logistics.util.DozerUtil;
import com.pzh.logistics.util.ResultVOUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping("/courier")
public class CourierController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private CourierService courierService;
    // 查看信息相关 ============================================

    //TODO 查看未派送的订单
    //       查看历史订单
    @RequiresRoles(RoleConstant.COURIER)
    @RequestMapping("/unCollect")
    public ResultVO unCollect(){
        User user = getLoginUser();
        return ResultVOUtil.success(courierService.listByState(user.getRoleRelativeId(), OrderConstant.NOT_COLLECT_NUM));
    }

    @RequiresRoles(RoleConstant.COURIER)
    @RequestMapping("/collect")
    public ResultVO collect(){
        User user = getLoginUser();
        return ResultVOUtil.success(courierService.listByState(user.getRoleRelativeId(), OrderConstant.COLLECT_NUM));
    }


    @RequiresRoles(RoleConstant.COURIER)
    @RequestMapping("/unDeliver")
    public ResultVO unDeliver(){
        User user = getLoginUser();
        return ResultVOUtil.success(courierService.listByState(user.getRoleRelativeId(), OrderConstant.NOT_DELIVER_NUM));
    }


    @RequiresRoles(RoleConstant.COURIER)
    @RequestMapping("/deliver")
    public ResultVO deliver(){
        User user = getLoginUser();
        return ResultVOUtil.success(courierService.listByState(user.getRoleRelativeId(), OrderConstant.DELIVER_NUM));
    }


    private User getLoginUser(){
        Subject subject = SecurityUtils.getSubject();
        return (User)subject.getPrincipal();
    }
}
